#include "cmdparse.h"
#include "randomizer.h"

const char* g_arrName = "node";

CmdParse::CmdParse(const QString &qstrFilename_) :
    m_fileName(qstrFilename_),
    m_pscriptValueIte(NULL)
{
    QFile _file(m_fileName);
    if (_file.open(QIODevice::ReadWrite))
    {
        m_buffer = "value = " + _file.readAll();
    }
    m_scriptValue = m_scriptEngine.evaluate(m_buffer);

}

CmdParse::~CmdParse()
{
    delete m_pscriptValueIte;
}


/////////////////////////////
///
/// if (xxx.IsOk())
/// {
///     while (xxx.HasNext())
///     {
///         yyy(GetNext());
///
///     }
/// } else {
///     error << "Parse Error!\n";
/// }
///
///
///
///
///
///
///
///
bool CmdParse::IsOk()
{
    bool _re = m_scriptValue.property(g_arrName).isArray();
    if (_re)
    {
        m_pscriptValueIte = new QScriptValueIterator(m_scriptValue.property(g_arrName));
    }

    return _re;
}


bool CmdParse::HasNext()
{
    if (!m_pscriptValueIte)
    {
        return false;
    }
    return m_pscriptValueIte->hasNext();
}

SimulatorCmd CmdParse::GetNext()
{
    SimulatorCmd _re;
    _re.m_nDataLenght = -1;
    if (!m_pscriptValueIte)
    {
        return _re;
    }

    m_pscriptValueIte->next();
    QString _strTemp = m_pscriptValueIte->value().property("type").toString();
    if (_strTemp.isEmpty())
    {
        return _re;
    }
    if ( _strTemp == "C")
    {
        _re.m_nodeFlag = SimulatorCmd::C;
    } else if (_strTemp == "R") {
        _re.m_nodeFlag = SimulatorCmd::R;
    } else {
        _re.m_nodeFlag = SimulatorCmd::E;
    }

    _re.m_qstrSendNetWorkID = m_pscriptValueIte->value().property("networkid").toString();
    _re.m_qstrParentNetWorkID = m_pscriptValueIte->value().property("parentnetworkid").toString();
    _re.m_qstrReceiveNetWorkID = m_pscriptValueIte->value().property("recivenetworkid").toString();
    _re.m_circleFlag = (SimulatorCmd::CIRCLE)m_pscriptValueIte->value().property("flag").toInt32();
    _re.m_cmdID = (SimulatorCmd::CMDID)m_pscriptValueIte->value().property("cmdid").toInt32();
    _re.m_qstrContext = m_pscriptValueIte->value().property("context").toString();
    _re.m_DeviceName = m_pscriptValueIte->value().property("name").toInt32();
    _re.m_nT = m_pscriptValueIte->value().property("Ttime").toInt32();
    _re.m_nCurrentSec = 0;
    _re.m_nDataLenght = 0;//-1为无效不启用


    return _re;
}
